﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using devtm.Collections;
using System.ComponentModel;

namespace devtm.Editor.Configuration.Customs
{
    public class CustomTypeCollection : INotifyPropertyChanged
    {


        #region Generate New Item

        public void Create(Type type, string path)
        {

            CustomType c;

            if (Types.ContainsKey(type.AssemblyQualifiedName))
                c = Types[type.AssemblyQualifiedName];
            else
                c = Types.Add(new CustomType() { Fullname = type.AssemblyQualifiedName, LastUpdate = DateTime.Now });

            c.PropertyChanged += c_PropertyChanged;

            c.Create(type, path);

        }

        #endregion


        #region properties

        private DateTime _LastUpdate;
        [System.Xml.Serialization.XmlAttribute()]
        public DateTime LastUpdate
        {
            get
            {
                return _LastUpdate;
            }
            set
            {
                if (_LastUpdate == value)
                    return;

                _LastUpdate = value;

                PropertyChange("LastUpdate");
            }
        }

        [System.Xml.Serialization.XmlIgnore()]
        public bool IsModified { get; set; }

        [System.Xml.Serialization.XmlAttribute()]
        public string Fullname { get; set; }

        public IndexedObservableCollection<CustomType, string>
           Types = new IndexedObservableCollection<CustomType, string>(c => c.Fullname);

        #endregion


        #region  Changed
       

        void c_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs f)
        {

            if (f.PropertyName == "LastUpdate")
                LastUpdate = (sender as CustomType).LastUpdate;

        }
       

        private void PropertyChange(string p)
        {

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));

            if (p != "LastUpdate")
                LastUpdate = DateTime.Now;

            IsModified = true;

        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        
    }
}
